package com.example.yblin.hello24;

import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yblin.hello24.DataUtils.NetUtil;
import com.example.yblin.hello24.DataUtils.RegisterUtil;

public class RegisterActivity extends AppCompatActivity {

    private ImageView btBack;
    private EditText etUsername;
    private EditText etPassword;
    private EditText etConfirm;
    private TextView btRegister;

    private Toast toast;

    private String username;
    private String password;
    private String confirm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        btBack = findViewById(R.id.back_register);
        btBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
        etUsername = findViewById(R.id.editText_register_username);
        etPassword = findViewById(R.id.editText_register_password);
        etConfirm = findViewById(R.id.editText_confirm_password);
        btRegister = findViewById(R.id.button_register);
        toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
        btRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (validate()) {
                    new RegisterAsync().execute();
                }
            }
        });
    }


    /**
     * 用于验证用户名密码是否输入
     */
    private boolean validate() {
        username = etUsername.getText().toString();
        password = etPassword.getText().toString();
        confirm = etConfirm.getText().toString();


        if (username == null || username.equals("")) {
            toast.cancel();
            toast = Toast.makeText(this, "请输入用户名！", Toast.LENGTH_SHORT);
            toast.show();
            return false;
        } else if (password == null || password.equals("")) {
            toast.cancel();
            toast = Toast.makeText(this, "请输入密码！", Toast.LENGTH_SHORT);
            toast.show();
            return false;
        } else if (confirm == null || confirm.equals("") || !password.equals(confirm)) {
            toast.cancel();
            toast = Toast.makeText(this, "请输入正确的确认密码！", Toast.LENGTH_SHORT);
            toast.show();
            return false;
        } else {
            for (int i = 0; i < username.length(); i++) {
                if (username.charAt(i) == '\'' || username.charAt(i) == '\"') {
                    toast.cancel();
                    toast = Toast.makeText(this, "用户名不合法！", Toast.LENGTH_SHORT);
                    toast.show();
                    return false;
                }
            }
        }
        return true;
    }


    public class RegisterAsync extends AsyncTask<Void, Void, Integer> {

        @Override
        protected void onPreExecute() {
            btRegister.setEnabled(false);
            btRegister.setText("正在注册");
        }

        @Override
        protected Integer doInBackground(Void... voids) {
            return RegisterUtil.register(username, password, RegisterActivity.this);
        }

        @Override
        protected void onPostExecute(Integer result) {
            btRegister.setEnabled(true);
            btRegister.setText("注册");
            if (result == NetUtil.CODE_OK) {
                startActivity(new Intent(RegisterActivity.this, MainActivity.class));
                finish();
            }
        }
    }
}